package com.sunlands.zlcx.usercenter.processor.online.impl;

import com.sunlands.zlcx.usercenter.client.SocketService;
import com.sunlands.zlcx.usercenter.client.bean.ShareMsgParamVO;
import com.sunlands.zlcx.usercenter.processor.online.OfflineProcessor;
import com.sunlands.zlcx.usercenter.service.UsersServiceImpl;
import com.sunlands.zlcx.usercenter.vo.UsersVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;

import static com.sunlands.zlcx.usercenter.constant.MessageType.USER_OFFLINE;
import static com.sunlands.zlcx.usercenter.constant.MessageType.USER_ONLINE;

/**
 * @author: anjl
 * @description:
 * @data: 2020-11-19
 **/
@Slf4j
@Component
public class OfflineMessageNoticeProcessor implements OfflineProcessor {

    @Override
    public void init(Long userId) {

        List<UsersVO> onlineFriends = usersService.findOnlineFriend(userId);
        if (onlineFriends.isEmpty()) {
            log.info("用户下线-没有在线的道友和关注他的人，不发送消息 userId = {}", userId);
            return;
        }

        onlineFriends.forEach(friend -> {
            ShareMsgParamVO message = ShareMsgParamVO.builder()
                    .type(USER_OFFLINE.getType())
                    .fromUserId(userId.toString())
                    .toId(friend.getId().toString())
                    .realToId(friend.getId())
                    .message("用户下线")
                    .build();
            log.info("发送socket消息-用户下线： message = {}", message);
            socketService.sendMessage(message);
        });
    }

    @Autowired
    private SocketService socketService;
    @Autowired
    private UsersServiceImpl usersService;
}
